<?php
namespace app\api\controller\m2979\auto;

use think\Db;
use service\ApiReturn;
use app\api\controller\Base;
use app\member\service\MemberCache;


class Freememberlist extends Base {
                
/**
 *  查询会员列表
 *@param  String[字符串]  xingming  姓名
 *@create_time  2023-12-21 10:23:03
 */ 
public function get_memberlist($data = [], $user = [])
{
    $headLang  = request()->header('lang');
    $where = [];
  
    $order = 'freememberlist.aid desc';
	if (!empty($data['sort_field'])) {
		$order = 'freememberlist.'.$data['sort_field'].' '.$data['sort_type'];
	}

    $where[] = ['freememberlist.is_del', '=', 0]; 
 	if (!empty($data['xingming'])) {
		$where[] = ['freememberlist.xingming', '=', $data['xingming']]; 
 	}

    $select_info = array (
  'xingbie' => 
  array (
    1 => '保密',
    2 => '男',
    3 => '女',
  ),
  'huiyuandengji' => 
  array (
    1 => '普通会员',
    2 => '白银会员',
    3 => '铂金会员',
    4 => '钻石会员',
  ),
); 
	$dataList = Db::table('lb_free_member_list')
		->alias('freememberlist')
		->field("freememberlist.xingming,freememberlist.nianling,freememberlist.xingbie,freememberlist.huiyuandengji")
		->where($where)
		->order($order)
		->find();
	if ($dataList){
		$infodataList = $dataList;
			$infodataList['xingbie_name'] = $select_info['xingbie']["{$infodataList['xingbie']}"] ?? '';
			$infodataList['huiyuandengji_name'] = $select_info['huiyuandengji']["{$infodataList['huiyuandengji']}"] ?? '';
		$dataList = $infodataList;
	}

    if($dataList){
        return ApiReturn::r(1, $dataList, '请求成功');
    }else{
        return ApiReturn::r(1, [], '暂无数据');
    }
}
            
/**
 *  查询会员列表
 *@param  String[字符串]  xingming  姓名
 *@create_time  2023-12-21 10:06:13
 */ 
public function get_memberlist_copy($data = [], $user = [])
{
    $headLang  = request()->header('lang');
    $where = [];
  
    $order = 'freememberlist.aid desc';
	if (!empty($data['sort_field'])) {
		$order = 'freememberlist.'.$data['sort_field'].' '.$data['sort_type'];
	}

    $where[] = ['freememberlist.is_del', '=', 0]; 
 	if (!empty($data['xingming'])) {
		$where[] = ['freememberlist.xingming', '=', $data['xingming']]; 
 	}

    $select_info = array (
  'xingbie' => 
  array (
    1 => '保密',
    2 => '男',
    3 => '女',
  ),
  'huiyuandengji' => 
  array (
    1 => '普通会员',
    2 => '白银会员',
    3 => '铂金会员',
    4 => '钻石会员',
  ),
); 
	$dataList = Db::table('lb_free_member_list')
		->alias('freememberlist')
		->field("freememberlist.xingming,freememberlist.nianling,freememberlist.xingbie,freememberlist.huiyuandengji")
		->where($where)
		->order($order)
		->select();
	foreach ($dataList as &$infodataList){
			$infodataList['xingbie_name'] = $select_info['xingbie']["{$infodataList['xingbie']}"] ?? '';
			$infodataList['huiyuandengji_name'] = $select_info['huiyuandengji']["{$infodataList['huiyuandengji']}"] ?? '';
	}

    if($dataList){
        return ApiReturn::r(1, $dataList, '请求成功');
    }else{
        return ApiReturn::r(1, [], '暂无数据');
    }
}
                
/**
 *  操作会员列表
 *@param  String[字符串]  xingming  姓名
 *@create_time  2023-12-21 11:04:44
 */ 
public function set_memberlist($data = [], $user = [])
{
    $return_data = [];
 
     		//入参判断会员列表
		if (!($data['xingming'] != '')) {
			throw new \Exception('错误',-1);
		}
		//查询会员列表
		$order = 'freememberlist.create_time desc';
		$where = [];
		$where[] = ['freememberlist.is_del', '=', 0]; 
 		if (isset($data['xingming'])) {
		$where[] = ['freememberlist.xingming', '=', $data['xingming']]; 
 		}
		$query_1 = Db::table('lb_free_member_list')
		->alias('freememberlist')
		->where($where)
		->order($order)
		->find();
		if (!($query_1)) {
		//添加会员列表
		$param=[];
		$param['create_id']= $user['id'];
		$param['xingming']= $data['xingming'];
		$param['nianling']= '18';
		$param['update_time'] = time();
		$param['create_time'] = time();
		$param['is_del'] = 0;
		$param['member_id'] = (isset($user['member_id']))?$user['member_id']:0;

		$query_2 = $param;
		$table_name = 'lb_free_member_list';
		$primary_key = 'aid';
		$res = Db::table($table_name)->insertGetId($param);
		if ($res) {
			$query_2['aid'] = $res;
		}
		if (!($res)) {
			throw new \Exception('错误',-1);
		}

		}
		 else{
		//更新会员列表
		$where = [];
		$where[] = ['is_del', '=', 0]; 
 		if (isset($data['xingming'])) {
		$where[] = ['xingming', '=', $data['xingming']]; 
 		}
		$param=[];
		$param['xingming']= $data['xingming'];
		$param['nianling']= 20;
		$param['update_time'] = time();

		$query_3 = $param;
		$table_name = 'lb_free_member_list';
		$primary_key = 'aid';
		$res = Db::table($table_name)->where($where)->update($param);
		if (!($res)) {
			throw new \Exception('错误',-1);
		}

		}

        
     $return_field_data = $query_3['aid'] ?? '';
		$return_data['aid'] = $return_field_data;
$return_field_data = $query_3['xingming'] ?? '';
		$return_data['xingming'] = $return_field_data;
$return_field_data = $query_3['nianling'] ?? '';
		$return_data['nianling'] = $return_field_data;
$return_field_data = $query_2['aid'] ?? '';
		$return_data['aid'] = $return_field_data;
$return_field_data = $query_2['xingming'] ?? '';
		$return_data['xingming'] = $return_field_data;
$return_field_data = $query_2['nianling'] ?? '';
		$return_data['nianling'] = $return_field_data;

     
     

    return ApiReturn::r(1, $return_data, '请求成功');
}

}